<?php
// Function to lookup authors, DAIs, research, and articles at NARCIS.

$narcis_url = 'http://sru.narcis.nl/sru';

# generic query function for narcis
function _narcis_do_query($query)
{
	// construct uri
	global $narcis_url;

	#$url = "{$narcis_url}?operation=searchRetrieve&version=1.1"
	#      ."&maximumRecords=10&recordSchema=metadata&query={$query}";
	$url = "{$narcis_url}?operation=searchRetrieve&version=1.1"
	      ."&recordSchema=metadata&query={$query}";

	#error_log("Fetching $url");
	#print "$url\n";

	// fetch content
	$fh = fopen($url,'r');
	if (!$fh) { throw new Exception("Couldn\'t open \`$url'"); }
	$rss = stream_get_contents($fh);
	fclose($fh);

	# evil hack: SimpleXML doesn't handle namespaces very sanely
	# so just remove the namespace crap
	$rss = preg_replace('/<(\/?)(\w+):/','<$1',$rss);

	// parse xml
	$tree    = new SimpleXMLElement($rss);
	$record = $tree->records->record;

	#print_r($tree);
	#print_r($record);

	if (!$record) return new SimpleXMLElement('<empty />');
	return $record;

}

# loop up a person's details on Narcis
function narcis_search_person($lastname,$initials)
{
	$lastname = urlencode($lastname);
	$initials = urlencode($initials);

	$query = $lastname;
	if ($initials) $query .= "+AND+$initials";
	$query .= "+AND+untokenized.collection+exact+person";

	$tree = _narcis_do_query($query);

	# if nothing was found, try again without initials
	if (!$tree and $initials) return narcis_search_person($lastname,'');

	#error_log("Found {$tree->count()} persons");

	# parse ugly XML atree into nice string array
	$result = array();
	for ($i=0; $i<$tree->count(); $i++)
	{
		$person = $tree[$i]->recordData->metadata->persoon;
		$info = array();

		$info['name']   = (string) $person->fullName;
		$info['link']   = (string) $person->url;
		$info['dai']    = (string) $person->dai;
		$info['exp']    = (string) $person->expertise_en;
		$info['field']  = '';
		$info['cat']    = '';
		if ($person->jobs)       $info['field']  = (string) $person->jobs->job->organisatie;
		if ($person->categories) $info['cat']    = (string) $person->categories->category->term[1];

		if ( preg_match('/^[0-9xX]{9,10}$/', $info['dai'] ) )
		{ 
			$info['dai'] = 'info:eu-repo/dai/nl/' . $info['dai'];
		}

		$result[$i] = $info;
	}

	return $result;
}


# loop up a person's details on Narcis by dai
function narcis_search_dai($dai)
{
	$shortdai = urlencode( preg_replace( '/^.*\/([0-9X]+)$/', '$1', $dai ) );
	$query = "$shortdai+AND+untokenized.collection+exact+person";

	#error_log("search for DAI $dai / $shortdai");

	$tree = _narcis_do_query($query);

	# parse ugly XML atree into nice string array
	$result = array();
	for ($i=0; $i<$tree->count(); $i++)
	{
		$person = $tree[$i]->recordData->metadata->persoon;
		$info = array();

		$info['name']   = (string) $person->fullName;
		$info['link']   = (string) $person->url;
		$info['dai']    = (string) $person->dai;
		$info['exp']    = (string) $person->expertise_en;
		$info['field']  = '';
		$info['cat']    = '';
		if ($person->jobs)       $info['field']  = (string) $person->jobs->job->organisatie;
		if ($person->categories) $info['cat']    = (string) $person->categories->category->term[1];

		if ( preg_match('/^[0-9xX]{9,10}$/', $info['dai'] ) )
		{ 
			$info['dai'] = 'info:eu-repo/dai/nl/' . $info['dai'];
		}

		$result[$i] = $info;
	}

	return $result[0];
}


?>
